package cn.miw.android.bdmp3;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import cn.miw.android.bdmp3.service.MusicService;
import cn.miw.android.bdmp3.service.Pub;

public class MainActivity extends TabActivity {
	private TabHost tabHost;
	private RadioGroup group;

	public static final String TAB_LOCAL = "tab_local";
	public static final String TAB_SEARCH = "tab_search";
	public static final String TAB_ONLINE = "tab_online";
	public static final String TAB_LIST = "tab_downlist";
	public static final String TAB_PLAYER = "TAB_PLAYER";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.maintabs);
		Pub.MusicComponent = new ComponentName(getApplicationContext(), MusicService.class);
		buildTabs();
	}

	private void buildTabs() {
		group = (RadioGroup) findViewById(R.id.main_radio);
		tabHost = getTabHost();

		Intent tabLocal  = new Intent(this, LocalActivity.class);
		Intent tabPlayer = new Intent(this, LocalActivity.class);
		Intent tabSearch = new Intent(this, LocalActivity.class);
		Intent tabOnline = new Intent(this, OnlineActivity.class);
		Intent tabList   = new Intent(this, LocalActivity.class);
		tabHost.addTab(tabHost.newTabSpec(TAB_LOCAL).setIndicator(TAB_LOCAL).setContent(tabLocal));
		tabHost.addTab(tabHost.newTabSpec(TAB_PLAYER).setIndicator(TAB_PLAYER).setContent(tabPlayer));
		tabHost.addTab(tabHost.newTabSpec(TAB_SEARCH).setIndicator(TAB_SEARCH).setContent(tabSearch));
		tabHost.addTab(tabHost.newTabSpec(TAB_ONLINE).setIndicator(TAB_ONLINE).setContent(tabOnline));
		tabHost.addTab(tabHost.newTabSpec(TAB_LIST).setIndicator(TAB_LIST).setContent(tabList));
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.button_about:
					tabHost.setCurrentTabByTag(TAB_PLAYER);
					break;
				case R.id.button_local:
					tabHost.setCurrentTabByTag(TAB_LOCAL);
					break;
				case R.id.button_online:
					tabHost.setCurrentTabByTag(TAB_ONLINE);
					break;
				case R.id.button_downlist:
					tabHost.setCurrentTabByTag(TAB_LIST);
					break;
				case R.id.button_search:
					tabHost.setCurrentTabByTag(TAB_SEARCH);
					break;
				default:
					break;
				}
			}
		});

		tabHost.setCurrentTabByTag(TAB_LOCAL);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			tabHost.setCurrentTabByTag(TAB_SEARCH);
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
}
